package test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class ArrayListVSLinkedList1 {
    static Random r = new Random();

//    public static void main(String[] args) {
//        ArrayList<Integer> arrayList = new ArrayList<>();
//        LinkedList<Integer> linkedList = new LinkedList<>();
//        int n = 1000_000;
//
//        System.out.println("-------------尾部插入" + n + "个元素-------------");
//        long time = insertTail(arrayList, n);
//        System.out.println("ArrayList插入" + n + "个元素耗时：" + time + "ms");
//        time = insertTail(linkedList, n);
//        System.out.println("LinkedList插入" + n + "个元素耗时：" + time + "ms");
//
//        System.out.println("-------------尾部删除" + n + "个元素-------------");
//        time = removeArrayLast(arrayList, n);
//        System.out.println("ArrayList删除" + n + "个元素耗时：" + time + "ms");
//        time = removeLinkedLast(linkedList, n);
//        System.out.println("LinkedList删除" + n + "个元素耗时：" + time + "ms");
//
//        System.out.println("-------------头部插入" + n + "个元素-------------");
//        insertArrayHead(arrayList, n);
//        System.out.println("ArrayList插入" + n + "个元素耗时：" + time + "ms");
//        insertLinkedHead(linkedList, n);
//        System.out.println("LinkedList插入" + n + "个元素耗时：" + time + "ms");
//
//
//        time = removeLinkedHead(linkedList, n);
//        System.out.println("LinkedList删除" + n + "个元素耗时：" + time + "ms");
//        time = removeArrayHead(arrayList, n);
//        System.out.println("ArrayList删除" + n + "个元素耗时：" + time + "ms");
//    }
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        LinkedList<Integer> linkedList = new LinkedList<>();
        int n=1000_000;
        Thread array = new Thread(() -> {
            test(arrayList, n);
        });
        Thread linked = new Thread(() -> {
            test(linkedList, n);
        });
        array.start();
        linked.start();
    }

    public static void test(ArrayList<Integer> arrayList, int n) {
        long start = System.currentTimeMillis();
        System.out.println("arrayList尾部插入" + n + "个元素:" + insertTail(arrayList, n)+ "ms");
        System.out.println("arrayList遍历" + n + "个元素:" + every(arrayList)+ "ms");
        System.out.println("arrayList尾部删除" + n + "个元素:" + removeArrayLast(arrayList, n)+ "ms");
        System.out.println("arrayList头部插入" + n + "个元素:" + insertArrayHead(arrayList, n)+ "ms");
        System.out.println("arrayList头部删除" + n + "个元素:" + removeArrayHead(arrayList, n)+ "ms");
        long end = System.currentTimeMillis();
        System.out.println("arrayList耗时：" + (end - start) + "ms");
    }

    public static void test(LinkedList<Integer> linkedList, int n) {
        long start = System.currentTimeMillis();
        System.out.println("linkedList尾部插入" + n + "个元素:" + insertTail(linkedList, n)+ "ms");
        System.out.println("linkedList遍历" + n + "个元素:" + every(linkedList)+ "ms");
        System.out.println("linkedList尾部删除" + n + "个元素:" + removeLinkedLast(linkedList, n)+ "ms");
        System.out.println("linkedList头部插入" + n + "个元素:" + insertLinkedHead(linkedList, n)+ "ms");
        System.out.println("linkedList头部删除" + n + "个元素:" + removeLinkedHead(linkedList, n)+ "ms");
        long end = System.currentTimeMillis();
        System.out.println("linkedList耗时：" + (end - start) + "ms");
    }

    public static long insertArrayHead(ArrayList<Integer> arrayList, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            arrayList.add(0, r.nextInt());
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long insertLinkedHead(LinkedList<Integer> linkedList, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            linkedList.addFirst(r.nextInt());
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    private static long removeLinkedHead(LinkedList<Integer> linkedList, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            linkedList.removeFirst();
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    private static long removeArrayHead(ArrayList<Integer> arrayList, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            arrayList.remove(0);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long removeLinkedLast(LinkedList<Integer> list, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.removeLast();
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long removeArrayLast(ArrayList<Integer> list, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.remove(list.size()-1);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }


    public static long insertTail(List<Integer> list, int n) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.add(r.nextInt());
        }
        long end = System.currentTimeMillis();
        return end - start;
    }
    public static long every(List<Integer> list){
        long start = System.currentTimeMillis();
        for (Integer i : list) {
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

}
